package com.wnxy.carlusermanageassembly.controller;


import com.wnxy.carl.api.UserApi;
import com.wnxy.carl.commons.JwtUtils;
import com.wnxy.carl.dto.MyMessageDTO;
import com.wnxy.carl.dto.RegisterDTO;
import com.wnxy.carl.dto.UserDTO;
import com.wnxy.carl.exception.DemoRuntimeException;
import com.wnxy.carl.param.*;
import com.wnxy.carl.response.Result;
import com.wnxy.carlusermanageassembly.entity.User;
import com.wnxy.carlusermanageassembly.mapper.UserMapper;
import com.wnxy.carlusermanageassembly.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2021-11-12
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController implements UserApi {

    @Resource
    private IUserService userService;

    @Resource
    private JwtUtils jwtUtils;

    @Override //密码登陆
    public Result passwordLogin(PasswordParam accountParam) {
        log.info("密码登陆功能：参数={}",accountParam);
        UserDTO userDTO = userService.accountLogin(accountParam);
        String jwt = jwtUtils.createJwtByUserId(120,userDTO.getUserId());
        return Result.success(String.class,"登陆成功",jwt);
    }

    @Override//短信登陆
    public Result shortMessageLogin(ShortMessageParam shortMessageParam) {
        log.info("手机登陆功能：参数= {}", shortMessageParam);
        UserDTO userDTO = userService.phoneLogin(shortMessageParam);
        String jwt = jwtUtils.createJwtByUserId(120,userDTO.getUserId());
        return Result.success(String.class,"登陆成功",jwt);
    }

    @Override//发送短信验证码 Boolean
    public Result sendMessageCode(String phone) {
        log.info("发送短信验证码功能：参数= {}",phone);
        if (null == phone) {
            return  Result.checkFail("手机号不能为空");
        }
        userService.phoneCode(phone);
        return Result.success("验证码发送成功");
    }

    @Override//验证手机号 Boolean
    public Result checkPhone(String phone) {
        log.info("验证手机号功能：参数= {}",phone);
        if (null == phone) {
            return  Result.checkFail("手机号不能为空");
        }
//        if(!ServiceImplUtil.chekePhone(phone)){
//            return  Result.checkFail("手机号格式不正确");
//        }
        User user = userService.findPhone(phone);
        if (null == user){
            throw new DemoRuntimeException("手机号不存在");
        }
        return Result.success("手机号存在");
    }

    @Override//注册账号 RegisterDTO
    public Result<RegisterDTO> register(RegisterParam registerParam) {
        log.info("注册账号功能：参数= {}",registerParam);
        RegisterDTO register = userService.register(registerParam);
        return Result.success(RegisterDTO.class,"注册成功",register);
    }

    @Override//修改密码 String
    public Result amendPassword(AmendPasswordParam amendPasswordParam) {
        log.info("修改密码功能：参数= {}",amendPasswordParam);
        userService.amendPassword(amendPasswordParam);
        return Result.success("修改成功");
    }

    @Override//查看我的信息 MyMessageDTO
    public Result<MyMessageDTO> myMessage(Integer userId) {
        log.info("查看我的信息功能：参数= {}",userId);
        User user = userService.findUserId(userId);
        MyMessageDTO myMessageDTO = new MyMessageDTO();
        BeanUtils.copyProperties(user,myMessageDTO);
        return Result.success(MyMessageDTO.class,"查询成功",myMessageDTO);
    }

    @Override//修改我的信息 MyMessageDTO
    public Result<MyMessageDTO> amendMessage(AmendMessageParam amendMessageParam) {
        log.info("修改我的信息功能：参数= {}",amendMessageParam);
        MyMessageDTO myMessageDTO = userService.amendMessage(amendMessageParam);
        return Result.success(MyMessageDTO.class,"修改成功",myMessageDTO);
    }

    @Override//验证身份证 Boolean
    public Result checkIdCard(String idcard) {
        log.info("验证身份证功能：参数= {}",idcard);
        User user = userService.chekeIdCard(idcard);
        if (null == user) {
            Result.success("身份证不存在");
        }
        return Result.success(User.class,"身份证查询成功",user);
    }

    @Override//修改积分，状态， UserDTO
    public Result<UserDTO> andminAmendUser(AndminAmendParam andminAmendParam) {
        log.info("修改积分，状态功能：参数= {}",andminAmendParam);
        UserDTO userDTO = userService.andminAmendUser(andminAmendParam);
        return Result.success(UserDTO.class,"修改成功",userDTO);
    }

    @Override//管理员查询用户信息 UserDTO
    public Result<UserDTO> andminSeeUser(Integer userId) {
        log.info("管理员查询用户信息功能：参数= {}",userId);
        UserDTO userById = userService.findUserById(userId);
        return Result.success(UserDTO.class,"查询成功",userById);
    }
}

